<?php
/**
 * Created by PhpStorm.
 * User: jsq
 * Date: 2017/12/2 0002
 * Time: 15:27
 */

namespace app\admin\controller;

use app\admin\logic\Activity as Activity_logic;
use think\Cache;
use think\Db;

class Activity extends Admin
{
    /**
     * 活动列表
     * @return mixed
     */
    public function index(){
        $Activity_logic = new Activity_logic();
        $activity = $Activity_logic->getAllActivitys();
//        dump($areas);
        $this->assign('activity',$activity);
        return $this->fetch();
    }

    /**
     * 新增活动
     * @return mixed
     */
    public function add(){
        if($this->request->isPost()){
            $data=input('post.');
            $Activity_logic = new Activity_logic();
            $result = $Activity_logic->addActivity($data);
            if($result['result'] == 1){
                $this->success($result['msg']);
            }else{
                $this->error($result['msg']);
            }
        }else{
            return $this->fetch();
        }
    }

    /**
     * 编辑活动
     * @return mixed
     */
    public function edit(){
        if($this->request->isPost()){
            $data=input('post.');
            //dump($data);
            $Activity_logic = new Activity_logic();
            $result = $Activity_logic->editActivity($data);
            //dump($result);
            if($result['result'] == 1){
                //$this->success($result['msg']);
                $this->success('修改成功');
            }else{
                $this->success('修改失败');
            }

        }else{
            $id=input('id');
            $activity = Db::name('activity')->where('id',$id)->find();
            $this->assign('activity',$activity);
            return $this->fetch();
        }
    }

    /**
     * 删除类别
     */
    public function del()
    {
        $id = $this->request->param('id', 0);
        $Activity_logic = new Activity_logic();
        $result = $Activity_logic->delActivity($id);
        if ($result['result'] === true) {
            $this->success($result['msg']);
        } else {
            $this->error($result['msg']);
        }
    }



    /**
     * 设置活动启用和禁用
     * @return \think\response\Json
     */
    public function setStatus(){
        $id = input('id');
        if(empty($id)){
            $this->error('ID丢失');
        }
        $info = Db::name('activity')->where('id',$id)->field('id,status')->find();
        $status = [1,0];
        $data['id'] = $id;
        $data['status'] = $status[$info['status']];
        $res = Db::name('activity')->update($data);
        if($res){
            Cache::rm('allActivitys');
            $this->success('设置成功');
        }else{
            $this->error('设置失败');
        }
    }

}